昨天講了我的計算機,分別是顯示數字與其他比較簡單的按鈕,今天要講比較難的部分,啊是對我比較難的部分啦,就是打結很久的地方,如果你有想過怎麼做的話,那來看看我們的想法是否一樣吧!
首先我先做一個think方法,通用加減乘除,就讓他自己判斷。
public void think(){
if(record == 0){
plus = temp;
num = 0;
tempplus = plus + num2;
plus = tempplus;
num2 = 0;
temp = plus;
}else if(record == 1){
if (temp == 0) {
negetive = temp;
num=0;
tempnegetive = num2;
}else {
negetive = temp;
num = 0;
tempnegetive = negetive - num2;
}
negetive = tempnegetive;
num2 =0;
temp = negetive;
}else if(record == 2){
if(temp == 0 && num2!=0){
temp = 1;
}
time = temp;
num = 0;
if(num2!=0) {
temptime = time * num2;
}else {
temptime = time;
}
time = temptime;
num2 = 0;
temp = time;
}else if(record == 3){
if(temp == 0){
temp = 1;
devide = temp;
num = 0;
tempdevide = num2 / devide;
}else {
devide = temp;
num = 0;
tempdevide = devide / num2;
}
devide = tempdevide;
num2 = 0;
temp = devide;
}
}
用record的數字判斷剛剛是按哪個運算符號,0是加號,1是減號,2是乘號,3是除號
public void plus(View view){
if (record == 5){
record = 0;
}
think();
recordplus = 0;
record = 0;
number.setText( temp+"+");
warming++;
warm();
}
5是起始值,所以會變成0,如果不是5會讓think做上個運算符號,再把record變成0,而think會做加號部分,把之前的數字和新抓到的數字num2相加,再把結果存起來,warm的部分之後再講
public void negetive(View view){
if (record == 5){
record = 1;
}
think();
recordnegetive = 0;
record = 1;
number.setText( temp + "-");
warming++;
warm();
}
think裡的做法就是,先判斷temp是否為0,是就讓tempnegetive等於num2,不是就tempnegetive = negetive - num2,最後數字存到temp裡。
今天先到這裡,明天來講剩下的東西吧